package com.atguigu.system.service.impl;
import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.RouterVo;
import com.atguigu.model.vo.SysUserQueryVo;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysUserMapper;
import com.atguigu.system.service.SysMenuService;
import com.atguigu.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author yqr
 * @since 2023-06-06
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public IPage<SysUser> getByConditionAndPage(Page<SysUser> pageInfo, SysUserQueryVo sysUserQueryVo) {
        return sysUserMapper.selectByPage(pageInfo, sysUserQueryVo);
    }

    @Override
    public void updateStatus(String id, Integer status) {
        SysUser sysUser = sysUserMapper.selectById(id);
        sysUser.setStatus(status);
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public SysUser getByName(String username) {
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getUsername, username);
        return sysUserMapper.selectOne(lqw);
    }

    @Override
    public Map<String, Object> getInfo(String username) {
        //根据用户名查询用户基本信息
        SysUser sysUser = this.getByName(username);
        String userId = sysUser.getId();

        //根据用户id查询用户菜单权限信息
        List<RouterVo> menuPermissionsList = sysMenuService.getUserMenuList(userId);

        //根据用户id查询用户按钮权限信息
        List<String> buttonPermissionsList = sysMenuService.getUserButtonList(userId);

        //map 封装信息
        Map<String,Object> map = new HashMap<>();
            //存放角色
            map.put("roles",new HashSet<>());
            //存放头像
            map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            //存放姓名
            map.put("name",sysUser.getName());
            //存放菜单权限数据
            map.put("routers",menuPermissionsList);

            //存放按钮权限数据
            map.put("buttons",buttonPermissionsList);
        return map;
    }
}
